package com.ifonly.utils;

import com.google.common.base.Strings;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-09 14:40
 * @since JDK 1.6
 */
public class RequestUtils {

    /**
     * 获取请求的路径
     *  在 ServletContext 目录长度不为 0 的情况下, 要去除 ServletContext 目录
     *
     * @param ctxLen ServletContext 目录长度
     * @param requestURI 包含ServletContext目录
     * @return 请求路径
     */
    public static String getActionUrl(int ctxLen, String requestURI) {
        String actionUrl;
        if (ctxLen == 0) {
            actionUrl = requestURI;
        } else {
            actionUrl = requestURI.substring(ctxLen);
        }
        if (Strings.isNullOrEmpty(actionUrl)) {
            actionUrl = "/";
        }
        return actionUrl;
    }

    /**
     * 判断是否为 ajax 请求
     *
     * @param request HttpServletRequest对象
     * @return true - 是ajax请求
     */
    public static boolean isAjax(HttpServletRequest request) {
        return request.getHeader("X-Requested-With") != null
                && "XMLHttpRequest".equals( request.getHeader("X-Requested-With"));
    }

    /**
     * 获取请求方式
     *
     * @param request HttpServletRequest对象
     * @return 请求方式
     */
    public static String getRequestMethod(HttpServletRequest request) {
        return request.getMethod();
    }

    /**
     * 请求是否为GET请求
     *
     * @param request HttpServletRequest对象
     * @return true - GET请求
     */
    public static boolean isGet(HttpServletRequest request) {
        return "GET".equalsIgnoreCase(request.getMethod());
    }

    /**
     * 请求是否为POST请求
     *
     * @param request HttpServletRequest对象
     * @return true - POST请求
     */
    public static boolean isPost(HttpServletRequest request) {
        return "POST".equalsIgnoreCase(request.getMethod());
    }

    /**
     * 判断是否为文件上传
     *
     * @param request HttpServletRequest对象
     * @return true - 是文件上传
     */
    public static boolean isMultipart(HttpServletRequest request) {
        String contentType=request.getContentType();
        if(contentType==null || !contentType.startsWith("multipart/form-data")) {
            return false;
        }
        return true;
    }

    public static Map<String, String[]> parseQueryString(String s, String encoding) throws UnsupportedEncodingException {
        if (Strings.isNullOrEmpty(s)) {
            return null;
        }
        s = URLDecoder.decode(s, encoding);

        String[] keyValues = s.split("&");
        if (keyValues.length > 0) {
            Map<String, String[]> map = new HashMap<String, String[]>();
            for (String keyValue: keyValues) {
                String[] kv = keyValue.split("=");
                if (kv.length == 2) {
                    String key = kv[0];
                    String value = kv[1];
                    String[] valArr = map.get(key);
                    if (valArr == null) {
                        valArr = new String[]{value};
                        map.put(key, valArr);
                    } else {
                        String[] newValArr = new String[valArr.length + 1];
                        System.arraycopy(valArr, 0, newValArr, 0, valArr.length);
                        newValArr[newValArr.length - 1] = value;
                        map.put(key, newValArr);
                    }
                }
            }
            return map;
        }

        return null;
    }

}
